package model.searchers.advancedSearchers;

import java.io.BufferedReader;
import java.io.IOException;

import model.searchers.GenericSearcher;
import model.searchers.linkedListSearcher.LinkedList;
import model.searchers.URLList;

public abstract class AdvancedGenericSearcher extends GenericSearcher {

	private boolean pageRank;
	
	/**
	 * Read the specified text-file and set data-field to a nested linked list created from the file
	 * 
	 * @param filename	file from which to create nested linked list
	 * @throws IOException when reading the file causes trouble
	 */
	protected void readFile(String filename) throws IOException {
		String line;
		String currentPage;

		BufferedReader infile = this.getDataFromFile(filename);
		line = infile.readLine(); // Read the first line

		
		if (line != null && line.startsWith("*PAGE:")) {
			currentPage = line.substring(6);
		} else
			//in lack of a URL in beginning of file
		{
			currentPage = "No URL";
		}
		line = infile.readLine();// Read the next line
		while (line != null) { // Exit if there is no more lines in the file
			if (line.startsWith("*PAGE:")) {
				currentPage = line.substring(6);
			} else {
				this.put(line, currentPage);
			}
			line = infile.readLine();// Read the next line
		}
		infile.close(); // Close the file
	}

	@Override
	public String[] formatResult(LinkedList l) {
		if (l == null)
			return null;
		URLList structuredResult = (URLList) l;
		
		if (this.getPageRank()) {
			structuredResult = this.sort(structuredResult);
		}

		URLList temp = structuredResult;
		int size = 0;
		// count the number of results
		while (temp != null) {
			size++;
			temp = (URLList) temp.getNext();
		}
		String[] result = new String[size];
		for (int i = 0; i < size; i++) {
			result[i] = structuredResult.getValue() + " ("
					+ structuredResult.getOccurrences() + ")";
			structuredResult = (URLList) structuredResult.getNext();
		}
		return result;
	}

	public URLList sort(URLList result) {
		URLList sortedResult = null;
		if (result == null) {
			return new URLList(null, null);
		} else {
			URLList current = result;
			int highestOccurence = 0;
			while (current != null) {
				if (current.getOccurrences() > highestOccurence) {
					highestOccurence = current.getOccurrences();
				}
				current = (URLList) current.getNext();
			}
			int i = 1;
			while (i <= highestOccurence) {
				current = result;
				while (current != null) {
					if (current.getOccurrences() == i) {
						URLList newResult = new URLList(current.getValue(), i,
								sortedResult);
						sortedResult = newResult;
					}
					current = (URLList) current.getNext();
				}
				i++;
			}
		}
		return sortedResult;
	}

	public void setPageRank(boolean rank) {
		this.pageRank = rank;
	}

	public boolean getPageRank() {
		return this.pageRank;
	}
	
	@Override
	public String toString() {
		String result = "Filename: " + this.getFileName() + "\r\nSearcher: " +this.getSearcherType() +  "\r\nBoolean Search: " + this.isBooleanSearch() + "\r\nSorted: " + this.getPageRank();
		return result;
	}
}
